<?php

include_once('logic/action/Action.php');
include_once('logic/view/authenticate_view.php');


class AuthenticateAction extends Action
{
	public function generateImageOld()
	{


		$letters = substr(str_shuffle("ACDJKMNOPSTVWXYZ"),0,4);

		$new_image = imagecreate(40,16);

		imagecolorallocate($new_image, 255, 255, 255);
		imagecolorallocate($new_image, 0, 0, 0);
		imagecolorallocate($new_image, rand(50,175), rand(125,225), rand(125,225));
		imagecolorallocate($new_image, rand(125,225), rand(50,175), rand(125,225));

		imageline($new_image, 0, rand(9,14), 40, rand(2,7), 2);
		imagestring($new_image, 5, 3, 0, " " . $letters[0] . " " . $letters[1], 1);
		imageline($new_image, 0, rand(2,7), 40, rand(9,14), 3);
		imagestring($new_image, 5, 3, 0, $letters[2] . " " . $letters[3], 1);

		Session::setSessionVar("md5", md5($letters[2] . $letters[0] . $letters[3] . $letters[1]));
		$pngName = date("dHis");
		imagepng($new_image,"tmp/$pngName.png");

		//header('Content-type: image/png');
		//imagepng($new_image);
		imageDestroy($new_image);
		return "tmp/$pngName.png";

	}

	public function generateImage()
	{

		$letters = substr(str_shuffle("ACDJKMNOPSTVWXYZ"),0,4);
		$code = $letters[2] . $letters[0] . $letters[3] . $letters[1];
		$size=8;
		$h=200;
		$w=400;
		$im = imagecreate($w, $h);
		$fontfile="tmp/AnkeCalligraph.TTF";

		$fill = ImageColorAllocate ( $im ,  251,  121, 34 );
		$light = ImageColorAllocate ( $im,  255,  255,  255 );
		$corners = ImageColorAllocate ( $im ,  153 , 153 ,  102 );
		$dark = ImageColorAllocate ( $im , 51, 51 , 0 );
		$black = ImageColorAllocate ( $im , 0, 0 , 0 );

		$colors[1] = ImageColorAllocate ( $im ,  255 , 255 ,  255 );
		$colors[2] = ImageColorAllocate ( $im ,  255*0.95 , 255*0.95 ,  255*0.95 );
		$colors[3] = ImageColorAllocate ( $im ,  255*0.9 , 255*0.9 ,  255*0.9 );
		$colors[4] = ImageColorAllocate ( $im ,  255*0.85 , 255*0.85 ,  255*0.85 );

		$c=1;
		$anz=10;
		$step=4/$anz;
		for($i=0;$i<$anz;$i+=1)
		{
			$size=rand(7,60);
			$x=rand(-10,360);
			$y=rand(-10,200);
			$color=$colors[$c];
			$c+=$step;
			ImageTTFText ($im, $size, 0, $x, $y, $color, $fontfile, $code);
		}

		ImageLine ( $im , 0 ,0, $w-1, 0, $light );
		ImageLine ( $im , 0 ,0, 0, $h-2, $light );
		ImageLine ( $im , $w-1,0, $w-1, $h, $dark );
		ImageLine ( $im , 0 ,$h-1, $w-1, $h-1, $dark );
		ImageSetPixel ( $im , 0 ,$h-1, $corners );
		ImageSetPixel ( $im , $w-1 ,0, $corners );

		Session::setSessionVar("md5", md5($letters[2] . $letters[0] . $letters[3] . $letters[1]));
		$pngName = date("dHis");
		imagepng($im,"tmp/$at_pngName.png");

		//header('Content-type: image/png');
		//imagepng($new_image);
		imageDestroy($im);
		return "tmp/$at_pngName.png";

	}

	public function clearDir($location)
	{
        if( !$dirhandle = opendir($location) )
        	return;

        while( false !== ($filename = readdir($dirhandle)) ) {
        	if( $filename != "." && $filename != ".." ) {
            	$filename = $location. "/". $filename;
                if(!unlink($filename)) {

                }
            }
        }

	}

	public function clearImage($location)
	{
    	if( $location != "." && $location != ".." ) {
        	$file = $location;
            @unlink($file);
        }

	}

	public function perform ()
	{
		$View = new AuthenticateView();
		$Accounts = new AccountsDAO();

		if (HttpRequest::getRequest('form_posted') != 'posted')
		{
			if (Session::getSessionVar ("logged_in") == 'true')
			{
				$View->setParam('_account_authenticated', 'true');
			} else {
				$View->setParam('_account_authenticated', 'false');
			}
		}

		if ((HttpRequest::getRequest('form_posted') != 'posted')&&($image==""))
		{
			$image = $this->generateImage();
			$View->setParam ('_image', $image);
		}

		// check if there was a post
		if (HttpRequest::getRequest('form_posted') == 'posted')
		{
			$valid = true;

			// get values
			$name = trim(HttpRequest::getRequest('name'));
			$token = trim(HttpRequest::getRequest('token'));
			$imagecode = trim(HttpRequest::getRequest('imagecode'));
			$image = trim(HttpRequest::getRequest('image'));


			// name empty
			if (Validator::isEmpty($name))
			{
				$View->setParam('_name_empty_error', 'true');
				$valid = false;
			}

			// token empty
			if (Validator::isEmpty($token))
			{
				$View->setParam('_token_empty_error', 'true');
				$valid = false;
			}

			// imagetext empty
			if (Validator::isEmpty($imagecode))
			{
				$View->setParam('_imagecode_empty_error', 'true');
				$valid = false;
			}

			if (md5(strtoupper($imagecode)) != Session::getSessionVar("md5"))
			{
				$View->setParam('_imagecode_valid_error', 'true');
				$valid = false;
			}

			if (md5(strtoupper($token)) != $Accounts->getToken($name))
			{
				$View->setParam('_token_valid_error', 'true');
				$valid = false;
			}

			// fill back common things
			$View->setParam('_id', $photoId);
			$View->setParam('_name', $name);
			$View->setParam('_token', $token);
			$View->setParam('_level', $level);
			$View->setParam('_imagecode', $imagecode);
			$View->setParam('_image', $image);

			if ($valid)
			{
				Session::setSessionVar("logged_in", 'true');
				Session::setSessionVar("logged_user", $name);
				$View->setParam('_account_authenticated', 'true');
				$this->clearImage($image);
			} else {
				Session::setSessionVar("logged_in", 'false');
				$this->clearImage($image);
				$image = $this->generateImage();
				$View->setParam ('_image', $image);
				$View->setParam('_account_authenticated', 'false');
			}
		}
		parent::setCommonParams($View);
		$View->display();
	}
}

?>
